#!/bin/sh


#colors
Y="\\#DFAF8F\\" 
W="\\#BEBEBE\\"
R="\\#FF0000\\"
y="\\#DFAF8F\\" 
w="\\#BEBEBE\\"
r="\\#FF0000\\"
b="\\#3F8EB4\\"



compteur_mail="/home/vincent/.config/wmfs/mail.tmp"
status_mail="/home/vincent/.config/wmfs/mail_status.tmp"
cpu_log="/home/vincent/.config/wmfs/cpu.log"

#function :
calc(){
    echo "`echo $1 | bc -l | cut -d. -f1`"
}

progressbar(){
#progressbar x y w h bordcolor bgcolor barcolor value maxvalue
	local BARH=`calc "$8/$9*$3"`
	
	if let $BARH 2>/dev/null #si erreur dans le calcul ...	
	then
		BARH=$BARH
	else
		BARH=0
	fi
	local BORD=" \b[`calc \"$1 - 1\"`;`calc \"$2 - 1\"`;`calc \"$3 + 2\"`;`calc \"$4 + 2\"`;$5]\ "
	local BGBAR=" \b[$1;$2;$3;$4;$6]\ "
	local VBAR=" \b[$1;$2;$BARH;$4;$7]\ "

	echo "$BORD$BGBAR$VBAR"
}

text(){
	echo " \s[$1;$2;$3;$4]\ "
}
bar(){
	echo " \b[$1;$2;$3;$4;$5]\ "
}


# RECUPERATION DES DONNEES ####################################################
HEURE=`date +"%H"`
MINUTE=`date +"%M"`
VOLUME=`amixer | grep "Master" -A 5 | grep -o "\[.*%" | sed "s/\[//" | sed 's/%//'`
#si le fichier est la on recup la valeur de la charge cpu
if [ -e $cpu_log ]
then
	CPU=`cat $cpu_log`
else
	CPU="err"
fi


# RAM  ###########################################
	 ram_free=$(echo "`cat /proc/meminfo | grep MemFree: | awk '{print $2}'`/1024" |bc)
	 ram_max=$(echo "`cat /proc/meminfo | grep MemTotal: | awk '{print $2}'`/1024" |bc)
	 ram_used=$(echo "${ram_max}-${ram_free}" | bc)
	 ram_used_percent=$(echo "${ram_used}*100/${ram_max}" | bc)
# RAM  ###########################################


# IRSSI ##########################################	
if [ -e ~/.irssi/fnotify ]
then
	HL=`tail -n1 ~/.irssi/fnotify | awk -F' ' '{ print $1 }' | sed 's/#//'`
	if [ "$HL" = "" ]
	then
		HL="No"
		HL_color=$y
	else
		#HL=$HL
		HL_color=$r
	fi
	HL=`printf "%-8.8s" "$HL"`
else
	HL="err"
fi
	
# IRSSI #########################################

# MPD ##########################################
MPD_STATUS=`mpc | grep playing` #variable pour savoir si mpd est en lecture 
	if [ -n "$MPD_STATUS" ] #si non vide ... mpd est lancé ... on affiche :)
	then
	#commenter les lignes inutilisées
	MPD_ARTISTE=`mpc | head -n1 | awk -F'-'   '{ print $1 }'`
	MPD_MORCEAU=`mpc | head -n1 | awk -F'-'   '{ print $2 }'`
#	MPD_VOLUME=`mpc  | tail -n1 | awk -F'   ' '{ print $1 }' | awk -F':' '{ print $2}' | sed 's/ //g'`
#	MPD_REPEAT=`mpc  | tail -n1 | awk -F'   ' '{ print $2 }' | awk -F':' '{ print $2}' | sed 's/ //g'`
#	MPD_RAMDOM=`mpc  | tail -n1 | awk -F'   ' '{ print $3 }' | awk -F':' '{ print $2}' | sed 's/ //g'`
#	MPD_SINGLE=`mpc  | tail -n1 | awk -F'   ' '{ print $4 }' | awk -F':' '{ print $2}' | sed 's/ //g'`
#	MPD_CONSUME=`mpc | tail -n1 | awk -F'   ' '{ print $5 }' | awk -F':' '{ print $2}' | sed 's/ //g'`
	MPD_ECOULE=`mpc | grep playing | awk '{print $4}'  | sed 's/(//' | sed 's/)//' | sed 's/%//'`
	
	MPD_AFFICHE="$MPD_ARTISTE - $MPD_MORCEAU"
	else
	MPD_AFFICHE="MPD~"
	fi   
MPD_AFFICHE=`printf "%-80.80s" "$MPD_AFFICHE"`
# MPD ########################################

# MOTIF #######################################################################
crochet_ouvr() {
local i=`echo "$1+2"|bc`
echo "\
`bar $1 0 2 15 $2`\
`bar $i 0 2 2 $2`\
`bar $i 13 2 15 $2`\
`bar $1 0 2 15 $2`\
"
}

crochet_ferm() {
local i=`echo "$1-2" |bc`
echo "\
`bar $1 0 2 15 $2`\
`bar $i 0 2 2 $2`\
`bar $i 13 2 15 $2`\
`bar $1 0 2 15 $2`\
"
}

deux_points(){
echo "\
`bar $1 5 1 1 $2`\
`bar $1 10 1 1 $2`\
"
}
bar_centrale(){
local i=`echo "$1-2" |bc`
local j=`echo "$1+2" |bc`
echo "\
`bar $1 0 2 15 $2`\
`bar $i 0 2 2 $2`\
`bar $i 13 2 15 $2`\
`bar $j 0 2 2 $2`\
`bar $j 13 2 15 $2`\
"
}


#rangement post-affichage ###################################################
horloge(){
local i=`echo "$1+4"|bc`
local j=`echo "$1+29"|bc`
local k=`echo "$1+32"|bc`
local l=`echo "$1+63"|bc`
echo "\
`crochet_ouvr $1 $b`\
`text $i 11 $w TIME`\
`deux_points $j $b`\
`text $k 11 $y "$HEURE $MINUTE"`\
`crochet_ferm $l $b`\
"
}
systemm(){
local bar_lar=35
local i=`echo "$1+4"|bc`
local j=`echo "$1+30"|bc`
local k=`echo "$1+35"|bc`
local l=`echo "$1+66"|bc`
local m=`echo "$1+$bar_lar+71"|bc`
local n=`echo "$1+$bar_lar+76"|bc`
local o=`echo "$1+$bar_lar+103"|bc`
local p=`echo "$1+$bar_lar+108"|bc`
local q=`echo "$1+$bar_lar+138"|bc`
local r=`echo "$1+$bar_lar+$bar_lar+143"|bc`
echo "\
`crochet_ouvr $1 $b`\
`text $i 11 $w CPU`\
`deux_points $j $b `\
`text $k 11 $y "$CPU%"`\
`progressbar $l 3 $bar_lar 8 '#999999' '#666666' '#DFAF8F' $CPU 100`\
`bar_centrale $m $b`\
`text $n 11 $w RAM`\
`deux_points $o $b `\
`text $p 11 $y "$ram_used_percent%"`\
`progressbar $q 3 $bar_lar 8 '#999999' '#666666' '#DFAF8F' $ram_used_percent 100`\
`crochet_ferm $r $b`\
"
}

irc_hl(){
local i=`echo "$1+4"|bc`
local j=`echo "$1+32"|bc`
local k=`echo "$1+35"|bc`
local l=`echo "$1+83"|bc`
echo "\
`crochet_ouvr $1 $b`\
`text $i 11 $w IRSSI`\
`deux_points $j $b`\
`text $k 11 $HL_color $HL`\
`crochet_ferm $l $b`\
"
}
#MUSIC 
music(){
local bar_lar=35
local i=`echo "$1+4"|bc`
local j=`echo "$1+30"|bc`
local k=`echo "$1+35"|bc`
local l=`echo "$1+66"|bc`
local m=`echo "$1+$bar_lar+71"|bc`
local n=`echo "$1+$bar_lar+76"|bc`
local o=`echo "$1+$bar_lar+103"|bc`
local p=`echo "$1+$bar_lar+108"|bc`
local q=`echo "$1+$bar_lar+$bar_lar+113"|bc`
local r=`echo "$1+$bar_lar+$bar_lar+430"|bc`
echo "\
`crochet_ouvr $1 $b`\
`text $i 11 $w VOL`\
`deux_points $j $b `\
`text $k 11 $y "$VOLUME%"`\
`progressbar $l 3 $bar_lar 8 '#999999' '#666666' '#DFAF8F' $VOLUME 100`\
`bar_centrale $m $b`\
`text $n 11 $w MPD`\
`deux_points $o $b `\
`progressbar $p 3 $bar_lar 8 '#999999' '#666666' '#DFAF8F' $MPD_ECOULE 100`\
`text $q 11 $y "$MPD_AFFICHE"`\
`crochet_ferm $r $b`\
"
}

# AFFICHAGE ##################################################################
wmfs -s "`horloge 365``systemm 435``irc_hl 655``music 835`"
exit 0